<template>
  <div>
    <top-simple-search
      @searchFun="searchTransitions"
      @addFun="editTransitions"
    ></top-simple-search>
    <el-table :data="transitionsList" stripe size="small" class="full-width" v-loading="loading">
      <el-table-column align="left" prop="name" label="名称">
        <template slot-scope="{ row }">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
        <el-table-column align="left" prop="order_id" label="源状态">
          <template slot-scope="{ row }">
            <span>{{ row.source_state_info.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="left" prop="type_id" label="目标状态">
          <template slot-scope="{ row }">
            <span>{{ row.destination_state_info.name }}</span>
          </template>
        </el-table-column>
      <my-dict-column dist-key="flow_button_type" align="left" prop="attribute_type_id" label="按扭类型"></my-dict-column>
      <my-dict-column dist-key="flow_show" align="left" prop="show" label="角色显示"></my-dict-column>
      <el-table-column
        align="left"
        prop="distribute_type_id"
        label="弹窗提示"
      >
        <template slot-scope="{ row }">
          <span v-if="row.alert_enable">是</span>
          <span v-if="!row.alert_enable">否</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="创建人">
        <template slot-scope="{ row }">
          <span>{{ row.creator }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="创建时间" width="170">
        <template slot-scope="{ row }">
          <span>{{ row.gmt_created }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column align="left" label="排序" width="170" prop="no_order" />-->

      <el-table-column align="left" fixed="right" label="操作" width="100">
        <template slot-scope="{ row }">
          <edit-button  @click="editTransitions(row)"></edit-button>
          <delete-button @click="dedTransitions(row.id)"></delete-button>
        </template>
      </el-table-column>
    </el-table>
    <transitionsAdd
      :visible.sync="transitionsVisible"
      @reloadList="getTransitionsList"
      :row="transitionsRow"
    ></transitionsAdd>
  </div>
</template>
<script>
import api from "@/api/index";
import qs from "qs";
import transitionsAdd from "../../components/transitionsAdd";
import MyDictColumn from '../../../../../components/TableColumn/MyDictColumn'
export default {
  components: {
    MyDictColumn,
    transitionsAdd
  },
  data() {
    return {
      id: this.$route.query.id,
      queryTransitions: "",
      //页码
      transitionsPage: {
        count: 0,
        page: 1,
        page_num: 10,
        total: 0,
        per_page: 10
      },
      //列表
      transitionsList: [],

      transitionsVisible: false,
      //源状态列表
      sourceStateList: [],
      //目标状态列表
      destinationStateList: [],

      transitionsId: null,

      transitionsRow: {},
      loading: false,
    };
  },
  created() {
    this.getTransitionsList();
  },
  methods: {
    searchTransitions(queryTransitions) {
      this.loading = true;
      this.queryTransitions = queryTransitions;
      api.searchTransitions(this.id, this.queryTransitions).then(res => {
          this.transitionsList = res.data;
        this.loading = false
      }).catch(() => {
        this.loading = false
      });
    },
    //获取流转列表
    getTransitionsList() {
      this.loading = true;
      api
        .getTransitions(
          this.id,
          qs.stringify({
            per_page: this.transitionsPage.per_page,
            page: this.transitionsPage.page
          })
        )
        .then(res => {
            this.transitionsList = res.data;
          this.loading = false
        }).catch(() => {
        this.loading = false
      });
    },

    //打开编辑或新增流转
    editTransitions(data) {
      this.transitionsVisible = true;
      if(data){
        this.transitionsRow = JSON.parse(JSON.stringify(data));
      }else{
        this.transitionsRow = {}
      }

    },

    dedTransitions(dedId) {
      this.$confirm("删除该流转, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        api.dedTransitions(this.id, dedId).then(res => {
            this.msgSuccess("删除成功!");
            this.getTransitionsList();
        });
      });
    }
  }
};
</script>
<style scoped>
.text {
  width: 100px;
}
.query {
  display: flex;
  align-items: center;
}
</style>
